#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include <cassert>

#include "../common/log.hpp"
#include "../common/util.hpp"

namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
    public:
        std::string number;     // 题目编号
        std::string title;      // 题目标题
        std::string difficulty; // 难度等级
        int cpu_limit;          // CPU运行时间限制S
        int mem_limit;          // 内存限制KB
        std::string desc;       // 题目描述
        std::string header;     // 题目预设给用户在线编辑器的代码
        std::string tail;       // 题目的测试用例，需要和header拼接，形成完整代码
    };

    const std::string questins_list = "./questions/questions.list";
    const std::string questins_path = "./questions/";
    const int COUNT = 5;

    class Model
    {
    private:
        std::unordered_map<std::string, Question> _questions; // 题目编号及题目详细信息的映射

    public:
        Model()
        {
            assert(LoadQuestionList(questins_list));
        }

        ~Model() = default;

        // 将所有题目加载到内存中
        bool LoadQuestionList(const std::string &question_list)
        {
            // 加载配置文件: questions/questions.list + 题目编号文件
            std::ifstream fin(question_list);
            if (!fin.is_open())
            {
                LOG(LOGLEVEL::FATAL)<<"加载题库失败,请检查是否存在题库文件\n";
                return false;
            }

            std::string line;

            while (std::getline(fin, line))
            {
                std::vector<std::string> tokens;
                StringUtil::SplitString(line, &tokens, " ");

                if (COUNT == tokens.size())
                {
                    Question q;

                    q.number = tokens[0];
                    q.title = tokens[1];
                    q.difficulty = tokens[2];
                    q.cpu_limit = atoi(tokens[3].c_str());
                    q.mem_limit = atoi(tokens[4].c_str());

                    std::string path = questins_path;
                    path += q.number;
                    path += "/";

                    FileUtil::ReadFile(path + "desc.txt", &(q.desc), true);
                    FileUtil::ReadFile(path + "header.cpp", &(q.header), true);
                    FileUtil::ReadFile(path + "tail.cpp", &(q.tail), true);

                    _questions.insert({q.number, q});
                }
                else
                {
                    LOG(WARNING) << "加载部分题目失败, 请检查文件格式\n";
                }
            }

            fin.close();

            LOG(INFO) << "加载题库成功!\n";
            return true;
        }

        // 获取所有题目信息
        bool GetAllQuestions(std::vector<Question> *out)
        {
            if(out==nullptr)
            {
                return false;
            }

            for( auto iter=_questions.begin();iter!=_questions.end(); ++iter )
            {
                out->push_back(iter->second);
            }

            return true;
        }

        // 根据题目编号获取指定题目信息
        bool GetOneQuestion(const std::string &number, Question *q)
        {
            auto iter=_questions.find(number);
            if(iter==_questions.end() || q==nullptr)
            {   
                return false;
            }
            (*q)=iter->second;

            return true;
        }
    };
}